package top.littlejiang.onlinexam.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.littlejiang.onlinexam.common.utils.JWTUtils;
import top.littlejiang.onlinexam.common.utils.Result;
import top.littlejiang.onlinexam.entity.OlexamStuAnswer;
import top.littlejiang.onlinexam.entity.OlexamStuScore;
import top.littlejiang.onlinexam.service.OlexamStuAnswerService;
import top.littlejiang.onlinexam.vo.CheckVO;
import top.littlejiang.onlinexam.vo.SubmitPaperVO;

import java.util.Map;

/**
 * @author chen
 * @Description
 * @since 2021/4/9 13:18
 */
@Controller
@RequestMapping("/stuanswer")
public class StuAnswerController {

    @Autowired
    OlexamStuAnswerService answerService;

    /**
     * 提交试卷
     * 创建自动阅卷任务
     * @param vo
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @PostMapping("/submitpaper")
    @ResponseBody
    public Result submitPaper(@RequestBody SubmitPaperVO vo) throws IllegalAccessException, InstantiationException {
        answerService.submitPaper(vo);
        return Result.ok();
    }

    /**
     * 查询成绩
     * 包含计算
     * @param param
     * @return
     */
    @PostMapping("/queryscore")
    @ResponseBody
    public Result queryScore(@RequestBody JSONObject param){
        String examUuid = param.getString("examUuid");
        OlexamStuScore stuScore = answerService.queryScore(examUuid, JWTUtils.getUserId());
        Map<String, Object> addup = answerService.addUp(examUuid);
        addup.put("totalScore", stuScore.getTotalScore());
        return Result.ok(addup);
    }

    /**
     * 获取一条待批阅数据
     * @param param
     * @return Result
     * @author chen
     * @since 2021/4/11 21:53
     */
    @PostMapping("/querystuanswer")
    @ResponseBody
    public Result stuAnswer(@RequestBody JSONObject param){
        String examUuid = param.getString("examUuid");
        CheckVO rst = answerService.queryToCheck(examUuid);
        return Result.ok(rst);
    }

    /**
     * 保存一条批阅记录
     * @param stuAnswer
     * @return
     */
    @PostMapping("/submitcheck")
    @ResponseBody
    public Result submitCheck(@RequestBody OlexamStuAnswer stuAnswer){
        answerService.submitCheck(stuAnswer);
        return Result.ok();
    }
}
